package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"sort"
)

func main() {
	params := map[string]interface{}{
		"name": "tom",
		"pwd":  "123456",
		"age":  30,
	}
	fmt.Printf("name=%v sign=%v", params["name"], createSign(params))
}

// MD5 方法
func MD5_2(str string) string {
	s := md5.New()
	s.Write([]byte(str))
	return hex.EncodeToString(s.Sum(nil))
}

func createSign(params map[string]interface{}) string {
	//定义一个数组
	key := []string{}
	//定义一个变量
	var str = ""
	for k := range params {
		key = append(key, k)
	}
	sort.Strings(key)
	for i := 0; i < len(key); i++ {
		if i == 0 {
			str = fmt.Sprintf("%v=%v", key[i], params[key[i]])
		} else {
			str = str + fmt.Sprintf("&xl_%v=%v", key[i], params[key[i]])
		}
	}
	var secret = "fkxq4vw50"
	sign := MD5_2(MD5_2(str) + MD5_2(secret))
	return sign
}
